package com.applet.applet_shangcheng.service;

import com.applet.applet_shangcheng.dao.TheRulesMapper;
import com.applet.applet_shangcheng.entity.Announcement;
import com.applet.applet_shangcheng.entity.ShufflingFigure;
import com.applet.applet_shangcheng.entity.TheProblem;
import com.applet.applet_shangcheng.entity.TheRules;
import com.applet.applet_shangcheng.utlis.DateUtils;
import com.applet.applet_shangcheng.utlis.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class TheRulesService {

    @Autowired
    private TheRulesMapper theRulesMapper;

    public int insertTheRules(TheRules theRules){
        int rules = theRulesMapper.insertTheRules(theRules);
        return rules;
    };

    public int updateTheRules(TheRules theRules){
        return theRulesMapper.updateTheRules(theRules);
    };

    public int updateStatus(int id,int status){
        return theRulesMapper.updateStatus(id,status);
    };

    public int deleteTheRules(int id){
        return theRulesMapper.deleteTheRules(id);
    };

    public List<TheRules> selectTheRules() {
        List<TheRules> theRulesList= theRulesMapper.getTheRules();
        return theRulesList;
    }
    public TheProblem findTheProblem(String synopsis){
        TheProblem theProblem = theRulesMapper.findTheProblem(synopsis);
        return theProblem;
    };

    public int updateTheProblem(TheProblem theProblem){
        int result = theRulesMapper.updateTheProblem(theProblem);
        return result;
    };

    public PageResult findAll(String queryString, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize );
        Page<TheRules> page = theRulesMapper.findAll(queryString);
        for (TheRules theRules : page) {
            if ( theRules.getContent()!=null&&  theRules.getContent().length()>20){
                String content = theRules.getContent();
                theRules.setContentT(content.substring(0,20));
            }else {
                theRules.setContentT(theRules.getContent());
            }
            if (theRules.getSynopsis()!=null && theRules.getSynopsis().length()>20){
                String synopsis = theRules.getSynopsis();
                theRules.setSynopsisT(synopsis.substring(0,20));
            }else {
                theRules.setSynopsisT(theRules.getSynopsis());
            }
        }
        PageInfo<TheRules> pageInfo = new PageInfo<>(page);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList() );
    }

    public TheRules findByTheRules(String id) {
        return theRulesMapper.findByTheRules(id);
    }

    public PageResult findTheProblemS(String queryString, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize );
        Page<TheProblem> page = theRulesMapper.findAlls(queryString);
        //PageInfo<Announcement> pageInfo = new PageInfo<>(announcementList);
        return new PageResult(page.getTotal(),page.getResult() );
    }
}
